package ru.xpparty.scanner;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * Created by IntelliJ IDEA.
 * User: Grab
 * Date: 19.12.10
 * Time: 15:48
 * To change this template use File | Settings | File Templates.
 */
public class SocketScanner {
    private String host;
    private int port;

    public SocketScanner(String host, int port) {
        this.host = host;
        this.port = port;
    }

    public PortStatus scan() {
        Socket socket = null;
        try {
        	socket = new Socket();        	
        	socket.connect(new InetSocketAddress(host, port));        	
            return PortStatus.OPENED;
        }
        catch(UnknownHostException e){
            throw new UnavailableHostException(e);
        }
        catch (IOException e) {
            return PortStatus.CLOSED;
        }
        finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
